package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"health/model/mysql"

	"health/registerrpc/internal/svc"
	"health/registerrpc/registerrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *registerrpc.UpdateRequest) (*registerrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	var update = mysql.Register{
		Model:  gorm.Model{ID: uint(in.Id)},
		Doctor: in.Doctor,
		Price:  float64(in.Price),
		Times:  in.Times,
		Types:  in.Types,
	}
	err := mysql.DB.Debug().Updates(&update).Error
	if err != nil {
		return nil, fmt.Errorf("修改失败")
	}
	return &registerrpc.UpdateResponse{Id: int64(update.ID)}, nil
}
